package com.x.erp.dao;

import com.x.erp.model.MultilevelActivityAccount;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 多级营销活动账户信息DAO
 */
@Repository
public class MultilevelActivityAccountDao extends BaseDao {

	private final static String selectMultilevelActivityAccountListSql = "SELECT " +
			"emaa.* " +
			"FROM " +
			"erp_multilevel_activity_account emaa " +
			"WHERE " +
			"1 = 1 ";
	/**
	 * 查询多级营销活动账户列表
	 * @param multilevelActivityAccount
	 * @return
	 * @throws Exception
	 */
	public List<MultilevelActivityAccount> selectMultilevelActivityAccountList(MultilevelActivityAccount multilevelActivityAccount) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityAccountListSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityAccount);
		return this.queryForList(sql.toString(), MultilevelActivityAccount.class, params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param multilevelActivityAccount
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, MultilevelActivityAccount multilevelActivityAccount) {
		if(multilevelActivityAccount != null) {
			if(multilevelActivityAccount.getUserId() != null){
				sql.append("AND emaa.USER_ID = ? ");
				params.add(multilevelActivityAccount.getUserId());
			}
		}
	}

	private final static String updateAddBalanceSql = "UPDATE erp_multilevel_activity_account SET ACCOUNT_BALANCE = ACCOUNT_BALANCE + ?, ACCOUNT_UPDATE_TIME = ? WHERE USER_ID = ? ";
	/**
	 * 修改余额
	 * @param userId
	 * @param detailMoney
	 * @return
	 */
	public int updateAddBalance(String userId, BigDecimal detailMoney) {
		List<Object> params = new ArrayList<>();
		params.add(detailMoney);
		params.add(this.getDate(0));
		params.add(userId);
		return this.execute(updateAddBalanceSql, params);
	}
}
